package maxinshiSomeipTool20.common.itfSafeSWT;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;

/**
 * 全工具通用的控件工厂：负责创建标准化的基础控件
 */
public class UiControlFactory {
    // ---------------------- 基础容器创建 ----------------------
    /**
     * 创建通用容器（默认2列布局，中等内边距）
     * @param parent 父容器
     * @param columnCount 列数（推荐用UiLayoutSpec.COL_*）
     * @param grabVertical 是否抢占垂直空间
     * @return 标准化容器
     */
    public static Composite createContainer(Composite parent, int columnCount, boolean grabVertical) {
        Composite container = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout(columnCount, false);
        layout.marginWidth = UiLayoutSpec.MARGIN_MD;
        layout.marginHeight = UiLayoutSpec.MARGIN_MD;
        layout.horizontalSpacing = UiLayoutSpec.GAP_MD;
        layout.verticalSpacing = UiLayoutSpec.GAP_MD;
        container.setLayout(layout);

        GridData data = new GridData(SWT.FILL, grabVertical ? SWT.FILL : SWT.TOP, true, grabVertical);
        container.setLayoutData(data);
        return container;
    }

    // ---------------------- 基础控件创建 ----------------------
    /** 创建标签（默认右对齐，固定宽度） */
    public static Label createLabel(Composite parent, String text) {
        Label label = new Label(parent, SWT.RIGHT);
        label.setText(text);
        GridData data = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
        data.widthHint = UiLayoutSpec.LABEL_WIDTH; // 固定宽度，确保对齐
        label.setLayoutData(data);
        return label;
    }

    /** 创建文本输入框 */
    public static Text createText(Composite parent, String defaultValue) {
        Text text = new Text(parent, SWT.BORDER);
        text.setText(defaultValue);
        GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
        data.heightHint = UiLayoutSpec.CONTROL_HEIGHT;
        text.setLayoutData(data);
        return text;
    }

    /** 创建下拉框（只读） */
    public static Combo createCombo(Composite parent, String[] items, int defaultIndex) {
        Combo combo = new Combo(parent, SWT.READ_ONLY);
        combo.setItems(items);
        if (defaultIndex >= 0 && defaultIndex < items.length) {
            combo.select(defaultIndex);
        }
        GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
        data.heightHint = UiLayoutSpec.CONTROL_HEIGHT;
        combo.setLayoutData(data);
        return combo;
    }

    /** 创建按钮 */
    public static Button createButton(Composite parent, String text) {
        Button button = new Button(parent, SWT.PUSH);
        button.setText(text);
        GridData data = new GridData(SWT.FILL, SWT.CENTER, false, false);
        data.heightHint = UiLayoutSpec.CONTROL_HEIGHT;
        button.setLayoutData(data);
        return button;
    }

    /** 创建表格（带列名） */
    public static Table createTable(Composite parent, String[] columnNames) {
        Table table = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        // 创建列
        for (String name : columnNames) {
            TableColumn column = new TableColumn(table, SWT.LEFT);
            column.setText(name);
            column.setWidth(150); // 默认列宽
        }
        // 表格布局（填满容器）
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
        data.heightHint = 200; // 最小高度
        table.setLayoutData(data);
        return table;
    }

    // ---------------------- 复合控件（常用组合） ----------------------
    /** 创建“标签+输入框”组合 */
    public static Text createLabeledText(Composite parent, String labelText, String defaultValue) {
        createLabel(parent, labelText); // 标签占第1列
        return createText(parent, defaultValue); // 输入框占第2列
    }

    /** 创建“标签+下拉框”组合 */
    public static Combo createLabeledCombo(Composite parent, String labelText, String[] items, int defaultIndex) {
        createLabel(parent, labelText);
        return createCombo(parent, items, defaultIndex);
    }
}
